<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="css/style.css" />
        <title>Your results</title>

        <script type="text/javascript" src="js/utils.js"></script>

        <?php

        //service function to color results
        function colorMe($score) {
            switch ($score) {
                case "1":
                    return "dontknow";
                    break;
                case "2":
                    return "onceortwice";
                    break;
                case "3":
                    return "know";
                    break;
                case "4":
                    return "friends";
                    break;
                default:
                    break;
            }
        }
        ?>
    </head>
    <body>
        <?php
        require_once 'inc/feedback.php';
        require_once 'inc/classifier.php';

        session_start();

//        $res;
//
//        if (isset($_SESSION["results"])) {
//            $res = $_SESSION["results"];
//            unset($_SESSION["results"]);
//        } else {
//            $res = array();
//        }
        //$feedback = Feedback::getInstance($res);

        if (isset($_GET["action"]) && ((strcmp($_GET["action"], "show") == 0))) {

            $feature_file = $_SESSION["features"];
            $friends = $_SESSION["friends"];
            $rand_ids = $_SESSION["rand_ids"];

            //get labels
            $labels = array();

            foreach ($rand_ids as $id) {
                array_push($labels, $_POST[$id]);
            }

            $results_file = 'results/results_' . time();
            write_to_file(csv_to_examples($labels, $feature_file, ',', array(1)), $results_file);

            $err;
            //calculate real results
            $res = classifier($results_file, "inc/data/model", $err);

            $res = array_combine($rand_ids, $res);

            arsort($res, TRUE);
            //print_r($res);
        ?>
            <h1>Step 3</h1>

            <div id="main">
                <div id="title">
                    <p>Here are the results we calculated</p>
                </div>
                <div id="filler"></div>
                <div class="result">
                <?php
                foreach ($res as $id => $label) {
                    $divid = $id . '-val';
                ?>
                    <div class="result line <?php echo colorMe($label) ?>">
                        <div class="result item friendpic"><img src="https://graph.facebook.com/<?php echo $id ?>/picture" /></div>
                        <div class="result item"><?php echo $friends[$id] ?></div>
                        <div class="result item" id="<?php echo $divid ?>">
                            <script type="text/javascript">
                                document.writeln(translateValue("<?php echo $label ?>"));
                            </script>
                        </div>
                    </div>
                    <div class="result line empty"></div>

                <?php
                }
                ?>
            </div>
            <div id="filler"></div>
            <p style="text-align: center"><b>Error rate (0/1):</b> <?php echo $err ?>%</p>
        </div>

        <a class="large awesome blue" href="index.php">&laquo; Let's do it again!</a>

        <?php
            }

            //in this case we ask the user to manually rank 20 friends
            else {

                $feature_file = $_SESSION["features"];
                $friends = $_SESSION["friends"];
                $rand_ids = $_SESSION["rand_ids"];
        ?>
                <h1>Step 2</h1>
                <div id="main">
                    <div id="title">
                        <p style="text-align: center; font-weight: bold">Please use the sliders to describe your relationship with the following Facebook friends</p>
                    </div>
                    <form action="results.php?action=show" method="POST">
                        <table align="center" class="label">
                    <?php
                    foreach ($rand_ids as $id) {
                        $divid = $id . '-val';
                    ?>
                        <tr>
                            <td><img src="https://graph.facebook.com/<?php echo $id ?>/picture" /></td>
                            <td><?php echo $friends[$id] ?></td>
                            <td>
                                <input type="range" min="1" max="4" name="<?php echo $id ?>" onchange="showValue('<?php echo $divid ?>',this.value)" value="2"/>

                            </td>
                            <td width="200px">
                                <div style="float:right" id="<?php echo $divid ?>">
                                    You meet occasionally
                                </div>
                            </td>
                        </tr>
                    <?php
                    }
                    ?>
                </table>
                <div style="text-align:center">
                    <input type="submit" value="Forward &raquo;" class="large awesome blue" />
                </div>
            </form>
        </div>
        <?php
                }
        ?>
    </body>
</html>
